package com.gxd.web.servlet;

import cn.dsna.util.images.ValidateCode;
import com.gxd.domain.Address;
import com.gxd.domain.User;
import com.gxd.service.AddressService;
import com.gxd.service.UserService;
import com.gxd.service.impl.AddressServiceImpl;
import com.gxd.service.impl.UserServiceImpl;
import com.gxd.util.Base64Utils;
import com.gxd.util.NotNull;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userservlet")
public class UserServlet extends ServletBase {
    UserService userService = new UserServiceImpl();
    AddressService addressService = new AddressServiceImpl();


    public String defaultAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String aid = request.getParameter("id");
        addressService.setDefaultAddress(aid,user.getId());

        return "forward:/userservlet?method=getAddress";

    }
        public String updateAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");

        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        int level = Integer.parseInt(request.getParameter("level"));
        Address address = new Address(id,detail,name,phone,user.getId(),level);
        addressService.updateAddress(address);
        return "forward:/userservlet?method=getAddress";
    }
        public String deleteAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        addressService.deleteById(id);

        return "forward:/userservlet?method=getAddress";
    }

    public String addAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        Address address = new Address(0,detail,name,phone,user.getId(),0);
        addressService.addAddress(address);
        return "redirect:/userservlet?method=getAddress";

    }
        public String getAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        List<Address> addList = addressService.findByUid(user.getId());
        request.setAttribute("addList",addList);
        return "forward:/self_info.jsp";
    }





    public String logOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().removeAttribute("user");
        Cookie cookie = new Cookie("autoinfo", "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        return "redirect:login.jsp";
    }
        public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String auto = request.getParameter("auto");

        if(NotNull.isEmpty(username)||NotNull.isEmpty(password)){
            return "redirect:/login.jsp";
        }
        User user=userService.login(username,password);
        if(user!=null){
            if("on".equalsIgnoreCase(auto)){
                String userinfo=username+"&"+password;
                Cookie cookie = new Cookie("autoinfo", Base64Utils.encode(userinfo));
                cookie.setMaxAge(14*24*60*60);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            request.getSession().setAttribute("user",user);
            return "redirect:/index.jsp";
        }else{
            return "redirect:/login.jsp";
        }

    }



        public void checkCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String code1 = request.getParameter("code");
        if(NotNull.isEmpty(code1)){
            response.getWriter().print("1");
            return;
        }
        String code2 = (String) request.getSession().getAttribute("code");
        if(code1.equalsIgnoreCase(code2)){
            response.getWriter().print("0");
        }else{
            response.getWriter().print("1");
        }
    }

    public void code(HttpServletRequest request, HttpServletResponse response) throws Exception{
        ValidateCode validateCode = new ValidateCode(100,50,2,100);
        request.getSession().setAttribute("code",validateCode.getCode());
        validateCode.write(response.getOutputStream());
    }
    public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        if(NotNull.isEmpty(username)){
            response.getWriter().print("0");
            return ;
        }

        boolean flag=userService.checkUserName(username);
        if(flag){
            response.getWriter().print("1");
        }else {
            response.getWriter().print("2");
        }
    }



    public String register(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        if(NotNull.isEmpty(username)|| NotNull.isEmpty(password)||NotNull.isEmpty(email)||NotNull.isEmpty(gender)){
            return "redirect:/register.jsp";
        }
        boolean flag=userService.checkUserName(username);
        if(flag){
            return "redirect:/register.jsp";
        }
        boolean flag1=userService.add(username,password,email,gender);
        if(flag1){
             return "redirect:/login.jsp";
        }else {
            return "redirect:/register.jsp";

        }
    }
}
